<?php

/**
 * 定义 Controller_Default 类
 *
 * @author sunnycoolboy
 * @package leadtone
 * @version $Id: User.php 2009-04-17
 */
class Controller_Default extends FLEA_Controller_Action {

    var $_modelClasses;
    var $_modelArticles;
    var $_modelAttachments;
    var $_tableMessages;
    var $_tableOrders;
    var $_tableConfigs;
    var $_helperFunction;
    var $productCid;
    var $newsCid;
    var $productDesCid;
    var $serviceCid;
    var $aboutCid;
    var $videoCid;
    var $imgCatId;
    var $oimgCatId;
    var $indexCaseId;
    var $photoCatId;
    var $dbo;

    function Controller_Default() {
        $this->_modelClasses = & FLEA::getSingleton('Model_Categories');
        $this->_modelArticles = & FLEA::getSingleton('Model_Articles');
        $this->_modelAttachments = & FLEA::getSingleton('Model_Attachments');
        $this->_tableMessages = & FLEA::getSingleton('Table_Messages');
        $this->_tableOrders = & FLEA::getSingleton('Table_Orders');
        $this->_tableConfigs = & FLEA::getSingleton('Table_Configs');
        $this->_helperFunction = & FLEA::getSingleton('Helper_Function');
        $this->productCid = PRODUCT_CLASS;
        $this->newsCid = NEW_CLASS;
        $this->productDesCid = PRODUCT_CATDES_CLASS;
        $this->serviceCid = SERVICE_CLASS;
        $this->generalCid = GENERAL_CLASS;
        $this->aboutCid = EZ_ABOUT_CLASS;
        $this->videoCid = VIDEO_CLASS;
        $this->imgCatId = INDEX_IMG_CLASS;
        $this->oimgCatId = OTHER_IMG_CLASS;
        $this->indexCaseId = INDEX_CASE_CLASS;
        $this->photoCatId = PHOTO_CLASS;
        $this->dbo = &FLEA::getDBO();
    }

    //首页
    function actionIndex() {
        $lunbo = $this->getActives(array('class_id' => $this->imgCatId), 5);
        $case1 = $this->getActives(array('class_id' => $this->oimgCatId), 6);
        $videos = $this->getActives(array('class_id' => $this->videoCid), 4);
        $case2 = $this->getActives(array('class_id' => $this->indexCaseId), 4);
        $teams = $this->getActives(array('class_id' => $this->newsCid), 6);
        $generals = $this->getActives(array('class_id' => $this->generalCid), 6);
        $mainTitle = 'index';
//        var_dump($generals);
//        dump($this->dbo->log);
        include(TPL_FRONT_DIR . '/main.php');
    }

    //作品首页
    function actionCase() {
        $pcid = $this->addsla($_REQUEST['cid']);
        $left = $this->getSubClasses($this->productCid);
        $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 0;
        $order = 'price,hot DESC,dateline DESC';
        $catid = $this->productCid;
        $field = array('class_id' => $catid);
        if ($pcid <= 0) {
            $pcid = $left[0]['class_id'];
        }
        if (is_numeric($pcid) && $pcid > 0) {
            $field['pcid'] = $pcid;
            $productDes = $this->getActives(array('class_id' => $this->productDesCid, 'pcid' => $pcid), 1);
        }
        FLEA::loadClass('FLEA_Helper_Pager');
        $table = & $this->_modelArticles->getTable();
        $pager = & new FLEA_Helper_Pager($table, $page, 6, $field, $order);
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*", true);
        $node = 'Case';
//		var_dump($productDes);
//		dump($this->dbo->log);
        include(TPL_FRONT_DIR . '/case.php');
    }

    //作品首页
    function actionVideo() {
        $pcid = $this->addsla($_REQUEST['cid']);
        $catid = $this->videoCid;
        $left = $this->getSubClasses($catid);
        $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 0;
        $order = 'hot DESC,dateline DESC';
        $field = array('class_id' => $catid);
        if ($pcid <= 0) {
            $pcid = $left[0]['class_id'];
        }
        if (is_numeric($pcid) && $pcid > 0) {
            $field['pcid'] = $pcid;
        }
        FLEA::loadClass('FLEA_Helper_Pager');
        $table = & $this->_modelArticles->getTable();
        $pager = & new FLEA_Helper_Pager($table, $page, 6, $field, $order);
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*", true);
        $node = 'Video';
//		var_dump($rowset);
//		dump($this->dbo->log);
        include(TPL_FRONT_DIR . '/video.php');
    }

    //作品最终页
    function actionWorkDetail() {
        $pid = isset($_GET['id']) ? (int) $_GET['id'] : 0;
        if (!$pid || !is_numeric($pid))
            js_alert('参数错误', '', $this->_url());
        $left = $this->getSubClasses($this->productCid);
        $product = $this->getActives(array('article_id' => $pid), 1);
        $product = $product[0];
        if (!$product)
            js_alert('参数错误', '', $this->_url());
        $table = & $this->_modelAttachments->getTable();
        $field = array('articleid' => $pid);
        $att = $table->findAll($field, '', 100);
//		var_dump($att);
//		dump($this->dbo->log);
        include(TPL_FRONT_DIR . '/workDetail.php');
    }

    //婚礼跟拍
    function actionPhoto() {
        $pcid = $this->addsla($_REQUEST['cid']);
        $left = $this->getSubClasses($this->photoCatId);
        $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 0;
        $order = 'price,hot DESC,dateline DESC';
        $catid = $this->photoCatId;
        $field = array('class_id' => $catid);
        if ($pcid <= 0) {
            $pcid = $left[0]['class_id'];
        }
        if (is_numeric($pcid) && $pcid > 0) {
            $field['pcid'] = $pcid;
            $productDes = $this->getActives(array('class_id' => $this->productDesCid, 'pcid' => $pcid), 1);
        }
        FLEA::loadClass('FLEA_Helper_Pager');
        $table = & $this->_modelArticles->getTable();
        $pager = & new FLEA_Helper_Pager($table, $page, 6, $field, $order);
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*", true);
        $node = 'Photo';
//		var_dump($productDes);
//		dump($this->dbo->log);
        include(TPL_FRONT_DIR . '/case.php');
    }

    //Contact页
    function actionContact() {
        $mainTitle = 'contact';
        include(TPL_FRONT_DIR . '/contact.php');
    }

    //about
    function actionAbout() {
        $about = $this->getActives(array('pcid' => $this->aboutCid), 1);
        $mainTitle = 'about';
        include(TPL_FRONT_DIR . '/about.php');
    }

    //new
    //NewDetail
    function actionNewDetail() {
        $fid = isset($_GET['nid']) ? (int) $_GET['nid'] : 0;
        if (!$fid || !is_numeric($fid)) {
            js_alert('参数错误', '', $this->_url());
        }
        $rowset = $this->getActives(array('article_id' => $fid), 1);
        $row = $rowset[0];
        if (!$row) {
            js_alert('参数错误', '', $this->_url());
        }
        $left = $this->getCurrentLevelClasses($row['pclasses']['class_id']);
        $class = $this->_modelClasses->getParentClasses($row['pclasses']);
        $catid = $class[1]['class_id'];
        switch ($catid) {
            case $this->newsCid:
                $node = 'Team';
                $front = 'newDetail.php';
                break;
            case $this->serviceCid:
                $node = 'Service';
                $front = 'general.php';
                break;
            case $this->generalCid:
                $node = 'General';
                $front = 'general.php';
                break;
            default :
                $node = 'Team';
                $front = 'newDetail.php';
        }
//        var_dump($row['pclasses']);
//	dump($this->dbo->log);
        include(TPL_FRONT_DIR . '/'.$front);
    }

    //service
    function actionNews() {
        $type = $this->addsla($_REQUEST['t']);
        switch ($type) {
            case 1:
                $catid = $this->newsCid;
                break;
            case 2:
                $catid = $this->serviceCid;
                break;
            case 3:
                $catid = $this->generalCid;
                break;
            default :
                $catid = $this->newsCid;
        }
        $pcid = $this->addsla($_REQUEST['cid']);
        if ($pcid) {
            $pclass = $this->_modelClasses->getClass($pcid);
            if ($pclass) {
                $catid = $pclass['parent_id'];
            }
        }
        switch ($catid) {
            case $this->newsCid:
                $cssClass = 'team';
                $node = 'Team';
                $classPic = 'banner_03.jpg';
                break;
            case $this->serviceCid:
                $cssClass = 'general';
                $node = 'Service';
                $classPic = 'service.jpg';
                break;
            case $this->generalCid:
                $cssClass = 'general';
                $node = 'General';
                $classPic = 'general.jpg';
                break;
            default :
                $cssClass = 'team';
                $node = 'Team';
                $classPic = 'banner_03.jpg';
        }
        $left = $this->getSubClasses($catid);
        $field = array('class_id' => $catid);
        if ($pcid <= 0) {
            $pcid = $left[0]['class_id'];
        }
        $field['pcid'] = $pcid;
        $nowClass = $this->_modelClasses->getClass($pcid);
        $page = isset($_GET['page']) ? (int) $_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
        $table = & $this->_modelArticles->getTable();
        $pager = & new FLEA_Helper_Pager($table, $page, 6, $field, 'hot DESC,dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*", true);
        $mainTitle = 'service';
//        var_dump($rowset);
//		dump($this->dbo->log);
        include(TPL_FRONT_DIR . '/new.php');
    }

    //转义
    function addsla($str) {
        return get_magic_quotes_gpc() ? $str : addslashes($str);
    }

    //验证码
    function actionImgCode() {
        $imgcode = & FLEA::getSingleton('Helper_ImgCode2');
        /* @var $imgcode Helper_ImgCode2 */
        $imgcode->image2();
    }

    /**
     * 获取文章
     */
    function getActives($field, $limit) {
        $table = &$this->_modelArticles->getTable();
        $rowset = $table->findAll($field, 'hot DESC,dateline DESC', $limit, '*', true);
//      dump($this->dbo->log);
        return $rowset;
    }

    function getSubClasses($catid) {
        $pclass = $this->_modelClasses->getClass($catid);
        $subclasses = $this->_modelClasses->getSubClasses($pclass);
        return $subclasses;
    }

    function getParentClasses($catid) {
        $pclass = $this->_modelClasses->getClass($catid);
        $parentclasses = $this->_modelClasses->getParentClasses($pclass);
        return $parentclasses;
    }

    function getCurrentLevelClasses($catid) {
        $pclass = $this->_modelClasses->getClass($catid);
        $classes = $this->_modelClasses->getCurrentLevelClasses($pclass);
        return $classes;
    }

    //html标签过滤
    function stripHtml($out, $_quota = false) {
        if (is_array($out)) {
            foreach ($out as $c_key => $c_val) {
                $out[$c_key] = $this->stripHtml($c_val, $_quota);
            }
        } else {
            $out = preg_replace("'<script[^>]*?>.*?</script>'si", '', $out);
            if (isset($_quota['quota_editor']) && $_quota['quota_editor'] == 'yes') {
                return($out);
            }
            $allw = '';
            if ($_quota['quota_html'] == 'yes' && strlen($_quota['quota_htmltags']) > 0) {
                $_all = explode('|', $_quota['quota_htmltags']);
                if (is_array($_all)) {
                    foreach ($_all as $tag) {
                        $allw .= "<{$tag}>";
                    }
                }
            }
            if (strlen($allw) > 0) {
                $out = strip_tags($out, $allw);
            } else {
                $out = strip_tags($out);
            }
        }
        return($out);
    }

    public function gohome() {
        redirect(url('Default'));
    }

    //获取访问者ip
    function getUserIp() {
        $ip = "";
        if ($_SERVER['HTTP_CLIENT_IP']) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif ($_SERVER['HTTP_X_FORWARDED_FOR']) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    function actionFxl() {
        $headers = 'Content-type: text/html; charset=utf8' . "\r\n";
        $headers .= 'From: coolsourcing@yahoo.com' . "\r\n" . 'Reply-To: coolsourcing@yahoo.com' . "\r\n" . "BCC: sunnycoolboy@sohu.com";
        $title = iconv('utf8', 'gbk', $title);
        $result = mail('sunnycoolboy@sina.com,sunnycoolboy@sohu.com', 'test 2to', '2to', $headers);
        var_dump($result);
    }

    function actionError() {
        $this->gohome();
    }

}